package com.tianqu.tms.www.member.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.webservice.weichat.api.MobileMessage;
import com.tianqu.tms.core.webservice.weichat.api.PullingExecutor;
import com.tianqu.tms.core.webservice.weichat.api.rpc.MemberAddBo;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.www.member.viewhelper.MemberInputViewHelper;

/**
 * 
 * 商户信息修改结果页Controller
 * 
 * @author tianshifu
 * 
 */
public class MemberUDBaseResultController extends TmsController {

	private static final String SYSPATH = "sysPath";//

	private Logger logger = Logger
			.getLogger(MemberUDBaseResultController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			// 从uc中获取修改后会员基本信息
			MemberInputViewHelper view = (MemberInputViewHelper) uc
					.getAttribute(TmsConstants.VIEWHELP);
			// 将viewhelp信息set到entity中
			Member member = Member.getInstance();
			member = setEntityFromView(member, view);
			
			String sysPath = (String) uc.getAttribute(SYSPATH);

			TmsUserProfile profile = uc.getUserProfile();
			member.setUpdateUserId(profile.getUniqueId());
			// 执行更新操作
			member.updateMemberBaseInfoByMemId(member, sysPath);		

			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
			
//          微信同步
//			UserManager userApi = UserManagerImpl.create();
//			//会员ID、车位总数
//			userApi.memberUpdateCarPort(view.getMemberId(), member.getCarPort());
			MobileMessage message = new MobileMessage();
			message.putValue("MemberId", String.valueOf(member.getMemId()));
			message.setMessage("TMS商户信息修改无法同步[商户信息修改]接口,商户ID为#MemberId#");
			
			MemberAddBo api = new MemberAddBo();
			api.setMobileMessage(message);
			api.setMemberId(String.valueOf(member.getMemId()));
			api.setRealName(member.getRealName());
			api.setShortName(member.getShortName());
			api.setMarketNo(TmsConstants.MARKET_NO);
			api.setRealNamePy(member.getRealNamePy());
			
			PullingExecutor.getInstance().submit(api);
			
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

	/**
	 * 将viewhelp信息set到entity中
	 */
	private Member setEntityFromView(Member entity, MemberInputViewHelper view) {
		entity.setMemId(Integer.parseInt(view.getMemberId()));
		entity.setRealName(view.getRealName());
		entity.setShortName(view.getShortName());
		entity.setRealNamePy(view.getRealNamePy());
		entity.setContactOwner(view.getContactOwner());
		entity.setContactPhone(view.getContactPhone());
		entity.setLegal(view.getLegal());
		entity.setPhone(view.getPhone());
		entity.setCardNo(view.getCardNo());
		entity.setOrgNo(view.getOrgNo());
		entity.setSettlePhone(view.getSettlePhone().replaceAll("-", ""));
		entity.setCarPortInstr(view.getCarPortInstr());
		entity.setOrgPhoto(view.getOrgPhoto());
		entity.setBusinessPhoto(view.getBusinessPhoto());
		entity.setCardPhoto(view.getCardPhoto());
		entity.setMemberZipCode(view.getMemberZipCode());
		if (view.getRegisteredCapital() == null
				|| "".equals(view.getRegisteredCapital())) {
			entity.setRegisteredCapital(0);
		} else {
			entity.setRegisteredCapital(Integer.valueOf(view.getRegisteredCapital()));
		}
		return entity;
	}
}
